package com.example.tourpal.widget;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.tourpal.R;
import com.example.tourpal.model.PrivateMessage;
import com.example.tourpal.util.MessageIconDrawable;

public class ClockItemView extends BaseHolderView<PrivateMessage> implements
		View.OnClickListener {
	private OnClickStatusViewListener mListener;
	private int mPosition;
	private RelativeLayout mleftLayout;
	private RelativeLayout mrightLayout;
	private TextView mReveiver_content;
	private TextView mSender_content;
	private TextView mleft_name;
	private TextView mright_name;
	private ImageView mLeftHead;
	private ImageView mRightHead;
	private SimpleDateFormat mClockTimeformat;
	private Date mDate = new Date();
	private Context mContext;

	public ClockItemView(Context paramContext) {
		super(paramContext);
		this.mContext = paramContext;
	}

	private Date getDate() {
		if (mDate == null)
			mDate = new Date();
		return mDate;
	}

	@SuppressLint("SimpleDateFormat")
	private String getTimeFormat(long paramLong) {
		if (mClockTimeformat == null)
			mClockTimeformat = new SimpleDateFormat("HH:mm:ss");
		getDate().setTime(paramLong);
		return mClockTimeformat.format(mDate);
	}

	public void bindItemView(View paramView, ViewGroup paramViewGroup,
			int paramInt, PrivateMessage paramClock, int userId) {
		MessageIconDrawable messageIcon = new MessageIconDrawable(
				mContext.getResources());
		messageIcon.setIsCircular(true);
		if (paramClock.getFromUserId() == userId) {// right
			this.mSender_content.setText(paramClock.getContent());
			this.mright_name.setText(paramClock.getSender());
			// this.mSender_content.setVisibility(View.VISIBLE);
			// this.mReveiver_content.setVisibility(View.GONE);
			messageIcon.setContactDetails(paramClock.getSender(),
					paramClock.getContent());
			// this.mLeftHead.setVisibility(View.GONE);
			// this.mRightHead.setVisibility(View.VISIBLE);
			this.mRightHead.setImageDrawable(messageIcon);
			this.mrightLayout.setVisibility(View.VISIBLE);
			this.mleftLayout.setVisibility(View.GONE);
		} else {// left
			this.mReveiver_content.setText(paramClock.getContent());
			this.mleft_name.setText(paramClock.getSender());
			// this.mReveiver_content.setVisibility(View.VISIBLE);
			// this.mSender_content.setVisibility(View.GONE);
			messageIcon.setContactDetails(paramClock.getSender(),
					paramClock.getContent());
			// this.mRightHead.setVisibility(View.GONE);
			// this.mLeftHead.setVisibility(View.VISIBLE);
			this.mLeftHead.setImageDrawable(messageIcon);
			this.mrightLayout.setVisibility(View.GONE);
			this.mleftLayout.setVisibility(View.VISIBLE);
		}
	}

	public View onAttach(Context paramContext) {
		View localView = LayoutInflater.from(paramContext).inflate(
				R.layout.clock_list_item, null);
		this.mleftLayout = ((RelativeLayout) localView
				.findViewById(R.id.left_layout));
		this.mrightLayout = ((RelativeLayout) localView
				.findViewById(R.id.right_layout));
		this.mReveiver_content = ((TextView) localView
				.findViewById(R.id.clock_text1_content));
		this.mSender_content = ((TextView) localView
				.findViewById(R.id.clock_text2_content));
		this.mLeftHead = (ImageView) (localView.findViewById(R.id.lefthead));
		this.mRightHead = (ImageView) (localView.findViewById(R.id.righthead));
		this.mleft_name = ((TextView) localView.findViewById(R.id.left_name));
		this.mright_name = ((TextView) localView.findViewById(R.id.right_name));
		return localView;
	}

	public void onClick(View paramView) {
		if (this.mListener != null) {
			PrivateMessage localClock = (PrivateMessage) paramView.getTag();
			this.mListener.onClick(this.mPosition, localClock);
		}
	}

	public void setListener(
			OnClickStatusViewListener paramOnClickStatusViewListener) {
		this.mListener = paramOnClickStatusViewListener;
	}

	public static abstract interface OnClickStatusViewListener {
		public abstract void onClick(int paramInt, PrivateMessage paramClock);
	}
}
